package top.gelonggld.model;

import top.gelonggld.constants.Cons;
import top.gelonggld.constants.Rex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by gelon on 2017/7/20.
 */
public class Method extends Paragraph{

    int methodStart;
    String methodIndent;
    String methodHead;
    String MethodType;

    public Method(int start, int end, String paragraphStr, int type) {
        super(start, end, paragraphStr, type);
    }


    @Override
    public void onPushDate(String paragraphStr) {
        super.onPushDate(paragraphStr);
        parseMethodHeader();
        parseMethodType();
    }

    private void parseMethodType() {
        for (Note note : notes){
            if(note.getNoteName().equals(Cons.GET_NOTE)){
                MethodType = Cons.GET;
            }else if(note.getNoteName().equals(Cons.POST_NOTE)){
                MethodType = Cons.POST;
            }else if(note.getNoteName().equals(Cons.DELETE_NOTE)){
                MethodType = Cons.DELETE;
            }else if(note.getNoteName().equals(Cons.PUT_NOTE)){
                MethodType = Cons.PUT;
            }else if(note.getNoteName().equals(Cons.ALL_NOTE)){
                MethodType = Cons.ALL;
            }
        }
    }

    public String getMethodType() {
        return MethodType;
    }



    private void parseMethodHeader() {
        for(OutAnno oa : outAnnos){
            Matcher matcher = Pattern.compile(Rex.METHOD_HEAD).matcher(oa.paragraphStr);
            if(matcher.find()){
                methodStart = matcher.start()+oa.start - start;
                methodHead = matcher.group();
                methodIndent = matcher.group(1);
            }

//            String methodHead = "public ResultTo update(@PathVariable(\"imgId\") int imgId, @RequestPart MultipartFile[] files) {";
//            String rex = "(((public)|(private)|(protected))\\s+)?[\\w\\d]+\\s+[\\w\\d]+\\s*\\(\\s*(@(\\w+)(\\(([^\\)]*)\\)\\s)?" +
//                    "\\s*[\\w\\d\\[\\]]+\\s+[\\w\\d\\[\\]]+\\s*,?)*\\s*";
//            Matcher matcher = Pattern.compile(rex).matcher(methodHead);
//            if(matcher.find()){
//                methodStart = matcher.start();
//                methodHead = matcher.group();
//                System.out.println(methodHead);
//            }

//            Matcher matcher = Pattern.compile(Rex.METHOD_PARAM).matcher(str);
//            while (matcher.find()){
//               String result = matcher.group();
//               System.out.println(result);
//            }


        }
    }

    public String getMethodIndent() {
        return methodIndent;
    }

    public int getMethodStart() {
        return methodStart;
    }

    public String getMethodHead() {
        return methodHead;
    }
}
